<?php

namespace App\Http\Requests;

use App\Http\Controllers\Utils\ResponseFactory;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class BaseRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }


    public function messages()
    {
        return [
            'required' => 'The :attribute is required',
            'size'     => 'The :attribut must be exactly :size',
            'between'  => 'The :attribute must be between :min - :max',
            'email'    => 'The :attribute must be a email',
            'max'     => 'The :attribute must greater then :max',
            'min'   => 'The :attribute must lessen then :min'
        ];
    }

    public function failedValidation(Validator $validator)
    {
        foreach ($validator->errors()->toArray() as $error){
            throw  new HttpResponseException(ResponseFactory::simpleResponse(false,implode($error),400));
        }
    }
}
